home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / B-C / C Servant™.cpt / C Servant™.rsrc / TEXT_135_aText.txt < prev    next >
Encoding:
Text File  |  1992-02-24  |  1.4 KB  |  28 lines

  1.  
  2. 8. Arithmetic
  3.  
  4.             The arithmetic operators are the usual `+‚Äô, `-‚Äô, `*‚Äô, and `/‚Äô (truncating integer division if the operands are both int), and the remainder or mod operator `%‚Äô:
  5.  
  6.             x = a%b;
  7.  
  8. sets x to the remainder after a is divided by b (i.e., a mod b). The results are machine dependent unless a and b are both positive.
  9.  
  10.             In arithmetic, char variables can usually be treated like int variables. Arithmetic on characters is quite legal, and often makes sense:
  11.  
  12.             c = c + ‚ÄòA‚Äô‚Äî‚Äòa‚Äô;
  13.  
  14. converts a single lower case ascii character stored in c to upper case, making use of the fact that corresponding ascii letters are a fixed distance apart. The rule governing this arithmetic is that all chars are converted to int before the arithmetic is done. Beware that conversion may involve sign-extension ‚Äî if the leftmost bit of a character is 1, the resulting integer might be negative. (This doesn‚Äôt happen with genuine characters on any current machine.)
  15.  
  16.             So to convert a file into lower case:
  17.  
  18.             main( ) {
  19.                         char c;
  20.                                  while( (c=getchar( )) != ‚Äò\0‚Äô )
  21.                                                 if( ‚ÄòA‚Äô<=c && c<=‚ÄôZ‚Äô )
  22.                                                 putchar(c+‚Äôa‚Äô-‚ÄôA‚Äô);
  23.                                                 else
  24.                                                 putchar(c);
  25.             }
  26.  
  27. Characters have different sizes on different machines. Further, this code won‚Äôt work on an IBM machine, because the letters in the ebcdic alphabet are not contiguous.
  28.